package com.example.example.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.example.example.R;
import com.example.example.fragment.FiveFragment;
import com.example.example.fragment.FoureFragment;
import com.example.example.fragment.OneFragment;
import com.example.example.fragment.ThreeFragment;
import com.example.example.fragment.TwoFragment;
import com.example.example.transforms.StackTransforms;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    ArrayList<Fragment> fragments;
    ViewPager viewPager;
    private int screenWidth;//屏幕宽度


    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FoureFragment foureFragment;
    private FiveFragment fiveFragment;
    private  WindowManager wm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         wm = this.getWindowManager();

        viewPager = (ViewPager) findViewById(R.id.account_viewpager);
        initFragment();

        MyViewPagerAdapter viewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragments, this);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setPageTransformer(true, new StackTransforms());

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                screenWidth = wm.getDefaultDisplay().getWidth(); //获取屏幕的宽度
                int nextPosition=position+1;
                int translationX = (screenWidth - positionOffsetPixels) / 2;
                int dibu=screenWidth - positionOffsetPixels;

                Log.e("tag","屏幕宽度="+screenWidth+"滑动距离="+positionOffsetPixels+"偏移量="+translationX);

               if(nextPosition==3){
                   FoureFragment fragment3= (FoureFragment) fragments.get(nextPosition);
                    if(fragment3.getRelativlaout()!=null){
                        fragment3.getRelativlaout().setTranslationX(translationX);  //设置偏移量
                        fragment3.getZongRe().setTranslationX(-dibu);
                    }
                }else if(nextPosition==4){
                    FiveFragment fragment4= (FiveFragment) fragments.get(nextPosition);
                    if( fragment4.getImageView()!=null){
                        fragment4.getImageView().setTranslationX(translationX);
                        fragment4.getZongRe().setTranslationX(-dibu);
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //首页Fragment的图片点击事件
        oneFragment.setImageClick(new OneFragment.onImageClick() {
            @Override
            public void oneImageClick(View view) {
                viewPager.setCurrentItem(1);
            }

            @Override
            public void twoImageClick(View view) {
                viewPager.setCurrentItem(2);
            }

            @Override
            public void threeImageClick(View view) {
                viewPager.setCurrentItem(3);
            }
        });
    }

    private void initFragment() {
        //初始化各fragment
        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        foureFragment = new FoureFragment();
        fiveFragment = new FiveFragment();

        //将fragment装进列表中
        fragments = new ArrayList<>();
        fragments.add(oneFragment);
        fragments.add(twoFragment);
        fragments.add(threeFragment);
        fragments.add(foureFragment);
        fragments.add(fiveFragment);

    }

    private class MyViewPagerAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> fragments;
        Context context;

        public MyViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments, Context context) {
            super(fm);
            this.fragments = fragments;
            this.context = context;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments == null ? 0 : fragments.size();
        }
    }


}
